if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        linshi_search: {
            payment_state: "",
            time_start: "",
            time_end: "",
            id: "",
            is_refund:null,
            pay_way:null
        },
        lishi_invoice:{
            id:"",
            status:""
        },
        linshi_id: null,
        linshi_arrid: [],
        queren_type: null,//2备注  3修改订单  4退款操作  5发票修改
        order_status: "",
        remark: "",
        set_status_string: '<option value="">请选择</option>',
        old_count: 0,
        time: 0,
        getAudio: function () {
            var audio = new Audio();
            audio.src = 'mp3/order.mp3';
            return audio;
        },
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        status_ajax: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/orders/ordersStatusBy",
                type: "post",
                data: {
                    token: get_cookie("key")
                },
                async: false,
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        console.log(res);
                        var status_string = '<option value="">全部</option>';
                        $.each(res.data, function (i, o) {
                            status_string += '<option value="' + o.id + '">' + o.names + '</option>';
                        });
                        $("#select1").empty().append(status_string);
                        $.each(res.message, function (i, o) {
                            houtai.set_status_string += '<option value="' + o.id + '">' + o.names + '</option>';
                        });
                    }
                }
            });
        },
        order_list_ajax: function (page_number) {
            if (!page_number) {
                houtai.time = new Date().getTime() / 1000;
                $("#order_hint").removeClass("btn-danger").find("span").text(0);
            }

            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + "index.php?s=admincms/Orders/Orderslist",
                type: "post",
                data: {
                    serach: houtai.linshi_search.id,
                    status: houtai.linshi_search.payment_state,
                    startime: houtai.linshi_search.time_start,
                    endtime: houtai.linshi_search.time_end,
                    is_refund:houtai.linshi_search.is_refund,
                    pay_way:houtai.linshi_search.pay_way,
                    page: n,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    houtai.table_string(res);
                    console.log(res);
                    houtai.old_count = 0;
                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                               //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.order_list_ajax(pageIndex);
                        }
                    });

                }
            });
        },
        table_string: function (res) {
            // 写入table
            var string_head =
                "<thead>" +
                "<tr>" +
                "<th>订单编号</th>" +
                "<th>商品明细</th>" +
                "<th>用户</th>" +
                "<th>实付 / 总费用</th>" +
                "<th>支付方式 <b class='glyphicon btn btn-default btn-xs glyphicon-sort pay_way'></b></th>" +
                "<th>发票</th>" +
                "<th>是否退款 <b class='glyphicon btn btn-default btn-xs glyphicon-sort is_refund'></b></th>" +
                "<th>订单状态</th>" +
                "<th>生成时间</th>" +
                "<th>操作</th>" +
                "</tr>" +
                "</thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var shop_string = "";
                    var beizhu_string = "";
                    $.each(o.orderList, function (i, o) {
                        shop_string += '<p> ' +
                            '<b>名称:</b><span class="item_name">' + o.names + '</span>' +
                            '<b>品牌:</b><span>' + o.brand + '</span>' +
                            '<b>规格:</b><span>' + o.specs + '</span>' +
                            '<b>数量:</b><span>' + o.amount + '</span>' +
                            '<b>单价:</b><span>' + o.price + '</span>' +
                            '</p>';
                    });
                    $.each(o.Remark, function (i, o) {
                        beizhu_string += '<p>' +
                            '<span class="fl">' + o.text + '</span>' +
                            ' <b style="color:#999;" class="fr">' + o.time + '</b> ' +
                            '</p>';
                    });
                    var _status = "";
                    var invoice_status="";
                    var voucher_img=o.voucher?"<img class='voucher_img' src='"+o.voucher.min_pic+"'>":"无";
                    switch (o.status) {
                        case "已下单":
                            _status = "<b class='down_order' style='font-weight: 900;'>已下单<b>";
                            break;
                        case "已支付":
                            _status = "<b class='confirm' style='font-weight: 900;'>已支付<b>";
                            break;
                        case "配货中":
                            _status = "<b class='readying' style='font-weight: 900;'>配货中<b>";
                            break;
                        case "已出库":
                            _status = "<b class='out_library' style='font-weight: 900;'>已出库<b>";
                            break;
                        case "已完成":
                            _status = "<b class='complete' style='font-weight: 900;'>已完成<b>";
                            break;
                        case "已取消":
                            _status = "<b class='cancle' style='font-weight: 900;'>已取消<b>";
                            break;
                    }
                    if(o.invoiceList){
                            invoice_status = "<b class='confirm' style='font-weight: 900;cursor: pointer;'>需要 <span class='glyphicon glyphicon-share-alt'></span><b>";
                    }else{
                        invoice_status = "<span class='readying' style='font-weight: 900;'>不需要<span>";
                    }
                    var user_remark = o.user_Remark[0] ? o.user_Remark[0].text : "无";
                    var carry = o.carry ? o.carry : "不需要";
                    var istuikuan=o.is_refund===1?"无":"已退款";
                    var offway_string = "";
                    var offway = o.offwhy === 0 ? "无" : o.offwhy;
                    if (o.status === "已取消") {
                        offway_string = '<p><span>取消原因：</span> <b>' + offway + '</b></p>';
                    }
                    var order_types, tatle;

                    if (o.order_type === 0) {
                        order_types = "普通订单";
                        tatle = (o.real_price === '' ? "0" : o.real_price) + " / " + o.total_price + "RMB";
                    } else {
                        order_types = "积分订单";
                        tatle = (o.real_price === '' ? "0" : o.real_price) + " / " + o.total_price + "积分";
                    }
                    var hostory = o.log.length !== 0 ? '<div class="item_history"> <div title="点击查看所有历史"> <b><span>修改人:</span><b class="outbound_tatle">' + o.log[0].name + '</b></b><span title="查看全部历史"class="btn btn-info btn-xs outbound_all">全部</span>  </div> <p>' + o.log[0].now_state + '</p> <p>' + o.log[0].time + '</p></div>' : "无";
                    string_body += '' +
                        '<tr>' +
                        '<td class="order_number" style="vertical-align: middle"> <b>' + o.orderid + '</b> </td> ' +
                        '<td class="order_detail">' + shop_string + '</td> ' +
                        '<td class="order_username">' + o.user_name + '</td>' +
                        ' <td class="order_price"><span>' + tatle + '</span></td> ' +
                        ' <td class="order_invoice">' +o.pay_way + '</td> ' +
                        ' <td class="order_invoice">' +invoice_status + '</td> ' +
                        ' <td class="order_pay_way"><span>' + istuikuan + '</span></td> ' +
                        '<td class="order_state">' + _status + '</td> ' +
                        '<td class="order_time">' + o.add_time + '</td> ' +
                        '<td class="order_operation"> <select name="" id="" class="form-control select_state">' + houtai.set_status_string + ' </select> <b class="btn btn-info btn-block Excel" style="margin-top:5px;">打印</b></td>' +
                        ' </tr>' +
                        ' <tr style="display:none;">' +
                        ' <td class="order_select"  style="vertical-align: middle">'+voucher_img+'</td>' +
                        ' <td style="display:none;" class="order_select order_number1">' + o.orderid + ' </td>' +
                        ' <td class="user_info"> ' +
                        '<p style="margin-bottom:6px;"><span>订单类型：</span> <b> ' +order_types + '</b></p> ' +
                        '<p><span>运费：</span> <b> ' +o.carry_price + '</b></p> ' +
                        '<p><span>首单满减：</span> <b> ' + Math.float_(o.discount,2) + '</b></p> ' +
                        '<p style="margin-bottom: 10px;"><span>优惠券：</span> <b> ' + Math.float_(o.coupon,2) + '</b></p> ' +
                        '<p><span>收件人：</span> <b> ' + o.Consignee_Name + '</b></p> ' +
                        '<p><span> 电话：</span> <b> ' + o.Consignee_Phone + '</b></p> ' +
                        '<p><span>收货地址：</span> <b> ' + o.Consignee_Address + '</b></p> ' +
                        '<p><span>支付方式：</span> <b> ' + o.pay_types + '</b></p> ' +
                        '<p><span>支付时间：</span> <b>' + o.pay_time + '</b></p>' +
                        '<p><span>配送方式：</span> <b>' + o.service_way + '</b></p>' +
                        '<p><span>收货时间：</span> <b>' + o.send_time + '</b></p> ' +
                        '<p><span>是否搬运：</span> <b>' + carry + '</b></p> ' +
                        '<p><span>用户备注：</span> <b>' + user_remark + '</b></p> ' + offway_string + '</td>' +
                        '<td colspan="6" style="width:20px;"><div class="input-group" style="width:100%;"> <input type="text" class="form-control mark_input"> <span class="input-group-addon btn_hover mark" style="cursor: pointer">备注</span> </div> <div style="text-align: start;font-size:12px"> <div style="color:#f10;">备注历史</div> <div class="beizhu_wrap">' + beizhu_string + '</div> </div></td> ' +
                        '<td colspan="2" class="order_hostory outbound_history">' + hostory + '</td> ' +
                        '</tr>';
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='11'>没有数据</td></tr>";
            }

            $("#order_table").empty().append(string_head + string_body);
        },
        //修改发票状态
        set_invoice_ajax:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/invoice/invoiceStatus",
                type: "post",
                data: {
                    status: houtai.lishi_invoice.status,
                    orderId: houtai.linshi_id,
                    invoiceId:houtai.lishi_invoice.id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $.each($("table .order_number1"), function () {
                          if($(this).text()===houtai.linshi_id){
                              var set_btn=$(this).siblings(".order_invoice_info").find(".invoice_set");
                             if(set_btn.hasClass("btn-success")){
                                 set_btn.removeClass("btn-success").addClass("btn-info").text("未完成");
                                 set_btn.parents("tr").prev().find(".order_invoice").empty().append("<b class='complete' style='font-weight: 900;'>已完成<b>");
                             }else{
                                 set_btn.removeClass("btn-info").addClass("btn-success").text("完成");
                                 set_btn.parents("tr").prev().find(".order_invoice").empty().append("<b class='confirm' style='font-weight: 900;'>未完成<b>");
                             }
                             houtai.hint("<p style='color:#0f0'>修改成功！！</p>");
                             $("#modal_wrap").fadeOut();
                          }
                        });
                    }
                },
                error:function () {
                    houtai.hint("请求失败!!!");
                }
            });
        },
        set_order_ajax: function () {
            //订单状态修改请求
                $.ajax({
                    url: ip + "index.php?s=admincms/orders/opto",
                    type: "post",
                    data: {
                        tp: 0,
                        status: houtai.order_status,
                        orderid: houtai.linshi_id,
                        token: get_cookie("key")
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            $.each($("table .order_number>b"), function (i, o) {
                                if ($(o).text() === houtai.linshi_id) {
                                    houtai.hint("<span style='color:#0f0;'>已修改订单<span style='color:#fff;'>" + houtai.linshi_id + "</span>为<span style='color:#f10;'>" + $(o).parents("tr").find(".select_state>option:selected").text() + "</span></span>");
                                    var _status = "";
                                    switch ($(o).parents("tr").find(".select_state>option:selected").text()) {
                                        case "配货中":
                                            _status = "<b class='readying' style='font-weight: 900;'>配货中<b>";
                                            break;
                                        case "已出库":
                                            _status = "<b class='out_library' style='font-weight: 900;'>已出库<b>";
                                            break;
                                        case "已完成":
                                            _status = "<b class='complete' style='font-weight: 900;'>已完成<b>";
                                            break;
                                        case "支付":
                                            _status = "<b class='confirm' style='font-weight: 900;'>已支付<b>";
                                            break;
                                    }
                                    $(o).parents("tr").find(".order_state").html(_status);

                                    $(o).parents("tr").next().find(".order_hostory").empty().append('' +
                                        '<div class="item_history"> ' +
                                        '<div title="点击查看所有历史">' +
                                        ' <b><span>修改人:</span><b class="inbound_tatle">' + $("#user_name").text() + '</b></b>' +
                                        '<span title="查看全部历史"class="btn btn-info btn-xs outbound_all">全部</span>' +
                                        '</div> ' +
                                        '<p>' + $(o).parents("tr").find(".select_state>option:selected").text() + '</p>' +
                                        ' <p>' + new Date().format("yyyy-MM-dd hh:mm") + '</p> ' +
                                        ' </div>');

                                    $(o).parents("tr").find(".select_state").val("");
                                    $("#modal_wrap").fadeOut();
                                }
                            });
                        } else if (res.resStatus === 10) {
                            houtai.hint(res.message);
                        }else{
                            houtai.hint(res.message);
                        }

                    },
                    error:function () {
                        houtai.hint("请求失败!!!");
                    }
                });
        },
        add_remark_ajax:function () {
                $.ajax({
                    url: ip + "index.php?s=admincms/orders/opto",
                    type: "post",
                    data: {
                        tp: 1,
                        orderid: houtai.linshi_id,
                        remark: houtai.remark,
                        token: get_cookie("key")
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            $.each($("table .order_number1"), function (i, o) {
                                if ($(o).text() === houtai.linshi_id) {
                                    $(o).parents("tr").find(".beizhu_wrap").prepend('<p>' +
                                        '<span class="fl">' + $(o).parents("tr").find(".mark_input").val() + '</span>' +
                                        ' <b style="color:#999;" class="fr">' + new Date().format("yyyy-MM-dd hh:mm") + '</b> ' +
                                        '</p>');
                                    houtai.hint("<span style='color:#0f0;'>备注成功</span>");
                                    $("#modal_wrap").fadeOut();
                                    return;
                                }
                            });
                        }


                    },
                    error:function () {
                        houtai.hint("备注添加失败！！");
                    }
                });
        },
        print_ajax: function (id) {
            $.ajax({
                url: ip + "index.php?s=admincms/Orders/Orderslist",
                type: "post",
                data: {
                    serach: id,
                    status: "",
                    startime: "",
                    endtime: "",
                    is_refund:"",
                    page: 1,
                    token: get_cookie("key")
                },
                success: function (res) {
                    houtai.print_string(JSON.parse(res));
                }
            });
        },
        print_string: function (res) {
            console.log(res);
            $("#print_table span").text("");
            if (res.resStatus === 0) {
                $(".print_id").text(res.data[0].orderid);
                $(".print_name").text(res.data[0].user_name);
                $(".print_order_time").text(res.data[0].add_time);
                $(".print_shoujianren").text(res.data[0].Consignee_Name);
                $(".print_dianhua").text(res.data[0].Consignee_Phone);
                $(".banyun").text(res.data[0].carry ? res.data[0].carry : "无");
                $(".shouhuoshijian").text(res.data[0].send_time);
                $(".shouhuodizhi").text(res.data[0].Consignee_Address);
                $(".payment_status").text(res.data[0].pay_way);
                $(".yunfei").text(res.data[0].carry_price);
                $(".huokuan").text(Math.float_(res.data[0].total_price - res.data[0].carry_price, 2));
                $(".first_").text("-"+Math.float_(res.data[0].discount,2));
                $(".coupon_").text("-"+Math.float_(res.data[0].coupon,2));
                $(".zonge").text(res.data[0].real_price === "" ? "0.00" : res.data[0].real_price);
                $(".beizhu").text((res.data[0].user_Remark[0] && res.data[0].user_Remark[0].text) ? res.data[0].user_Remark[0].text : "无");
                $(".zhibiaoren").text($("#user_name").text());
                $(".payment_types").text(res.data[0].pay_types);
                var thead = "<thead> <tr> <th>商品编号</th> <th>商品名称</th> <th>品牌</th> <th>规格</th> <th>数量</th> <th>单价</th> </tr> </thead>";
                var tbody = "";
                $.each(res.data[0].orderList, function (i, o) {
                    tbody += '<tr>' +
                        '<td>' + o.pid + '</td>' +
                        '<td>' + o.names + '</td>' +
                        '<td>' + o.brand + '</td>' +
                        '<td>' + o.specs + '</td>' +
                        '<td>' + o.amount + '</td>' +
                        '<td>' + o.price + '</td>' +
                        '</tr>';
                });

                $("#print_table table").empty().append(thead + tbody);
                $("#print_table").show();
                $("#print_table>div").jqprint();
                $("#print_table").hide();


            }
        },
        // 新订单请求
        new_order_ajax: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/orders/orderMessage",
                type: "post",
                data: {
                    time: houtai.time,
                    token: get_cookie("key")
                },
                success: function (_res) {
                   var res = JSON.parse(_res);
                    console.log(res);
                    if (res.resStatus === 0) {

                        if (houtai.old_count !== res.data[0].count) {
                            houtai.getAudio().play();
                            if(IsPC()){
                                Push.create('新消息!', {
                                    body: res.data[0].count + '个新订单请查阅 !',
                                    icon: 'images/logo.png',
                                    timeout: 5000,
                                    onClick: function () {
                                        window.focus();
                                    }
                                });
                            }

                            $("#order_hint").removeClass("btn-primary").addClass("btn-danger").find("span").text(res.data[0].count);
                            houtai.old_count = res.data[0].count;
                        }
                    }

                },
                error:function () {
                    houtai.hint("新订单请求失败！！");
                }
            });
        },

        // 请求记录
        order_history_ajax: function (pages) {
            var n = pages ? pages : 1;
            $.ajax({
                url: ip + "index.php?s=admincms/orders/log",
                type: "post",
                data: {
                    orderid: houtai.linshi_id,
                    page: n,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    houtai.order_history_string(res);
                    $("#page_inbound").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.order_history_ajax(pageIndex);
                        }
                    });

                },
                error:function () {
                    houtai.hint("请求历史失败！！");
                }
            });
        },
        //记录写入table
        order_history_string: function (res) {
            console.log(res);
            var data = res.data;
            // 写入table
            var string_body = "";
          if (res.resStatus === 0) {
                $.each(data, function (i, o) {
                    var old_status = "";
                    var now_status = "";
                    switch (o.ever_state) {
                        case "已下单":
                            old_status = "<b class='down_order' style='font-weight: 900;'>已下单</b>";
                            break;
                        case "已支付":
                            old_status = "<b class='confirm' style='font-weight: 900;'>已支付</b>";
                            break;
                        case "配货中":
                            old_status = "<b class='readying' style='font-weight: 900;'>配货中</b>";
                            break;
                        case "已出库":
                            old_status = "<b class='out_library' style='font-weight: 900;'>已出库</b>";
                            break;
                        case "已完成":
                            old_status = "<b class='complete' style='font-weight: 900;'>已完成</b>";
                            break;
                        case "已取消":
                            old_status = "<b class='cancle' style='font-weight: 900;'>已取消</b>";
                            break;
                    }
                    switch (o.now_state) {
                        case "已下单":
                            now_status = "<b class='down_order' style='font-weight: 900;'>已下单</b>";
                            break;
                        case "已支付":
                            now_status = "<b class='confirm' style='font-weight: 900;'>已支付</b>";
                            break;
                        case "配货中":
                            now_status = "<b class='readying' style='font-weight: 900;'>配货中</b>";
                            break;
                        case "已出库":
                            now_status = "<b class='out_library' style='font-weight: 900;'>已出库</b>";
                            break;
                        case "已完成":
                            now_status = "<b class='complete' style='font-weight: 900;'>已完成</b>";
                            break;
                        case "已取消":
                            now_status = "<b class='cancle' style='font-weight: 900;'>已取消</b>";
                            break;
                    }

                    string_body += '<tr>' +
                        '<td><p style="line-height:41px;font-size:20px;"><span style="color:#f10;">' + o.name + '</span> 把订单从 ' + old_status + ' 修改为 ' + now_status + '<span style="float:right;color:#ccc;">' + o.time + '</span></p></td>';
                });
            } else {
                string_body = "<tr class='text-center'><td>没有找到哦 亲！</td></tr>";
                houtai.hint("没有找到哦 亲!");
            }

            $("#table_history").empty().append(string_body);
            $("#repertory_table_history").fadeIn();
            $(window).scrollTop(0);
        },
       // 退款
        tuikuan_ajax:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/orderrefund/orderRefund",
                type: "post",
                data: {
                    orderid : houtai.linshi_id,
                    token : get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    if (res.resStatus === 0) {
                        houtai.hint("<span style='color:#0f0;'>修改成功！！</span>");
                        houtai.order_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    }else{
                        houtai.hint(res.message);
                    }

                },
                error:function () {
                    houtai.hint("请求失败！！");
                }
            });
        }
    };
    setInterval(function () {
        houtai.new_order_ajax();
    }, 5000);
    houtai.status_ajax();
    houtai.linshi_search.id=location.search.match(/\d+/)&&location.search.match(/\d+/)[0];
    houtai.order_list_ajax();
    //一级分类
    $("#select1").bind("change", function () {
        houtai.linshi_search.payment_state = $(this).val();
        houtai.linshi_search.time_start = "";
        houtai.linshi_search.time_end = "";
        houtai.linshi_search.id = "";
        houtai.linshi_search.pey_way = null;
        houtai.linshi_search.is_refund = null;
        houtai.order_list_ajax();
    });
    $("#order_hint").bind("click", function (e) {
        $(this).removeClass("btn-danger").find("span").text(0);
        houtai.linshi_search.payment_state = "";
        houtai.linshi_search.time_start = "";
        houtai.linshi_search.time_end = "";
        houtai.linshi_search.id = "";
        houtai.linshi_search.pey_way = null;
        houtai.linshi_search.is_refund = null;
        houtai.order_list_ajax();
        e.preventDefault();
    });
    $('#input_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });

    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });
    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
    //点击展开订单信息
    $("#order_table").on("click", ".order_number>b", function (e) {
        $(this).parents("tr").next().fadeToggle();
    });
    //商品名 点击跳转到库存
    $("#order_table").on("click", ".item_name", function () {
        var string=$(this).text();
        for(var a=0,string_n="";a<string.length;a++){
           string_n+=String(string.charCodeAt(a)+100000).slice(1);
        }
        location.href="repertory_admin.html?"+string_n;
    });
    // 点击跳转到发票
    $("#order_table").on("click", ".order_invoice>b", function () {
        location.href="caiwu_admin.html?"+$(this).parents("tr").find(".order_number>b").text();
    });
    // 点击筛选支付方式
    $("#order_table").on("click", ".pay_way", function () {
        houtai.linshi_search.pay_way=houtai.linshi_search.pay_way===4?0:4;
        houtai.linshi_search.is_refund=null;
        houtai.order_list_ajax();
    });
    // 点击筛选是否退款
    $("#order_table").on("click", ".is_refund", function () {
        houtai.linshi_search.is_refund= houtai.linshi_search.is_refund===0?1:0;
        houtai.linshi_search.pay_way=null;
        houtai.order_list_ajax();
    });
    //订单发票修改
    $("#order_table").on("click", ".invoice_set", function () {
        houtai.linshi_id = $(this).parents("tr").find(".order_number1").text();
        houtai.queren_type =5;
        houtai.lishi_invoice.id=$(this).prev().text();
        if($(this).hasClass("btn-success")){
            houtai.lishi_invoice.status=1;
            $("#modal_wrap").find(".modal-body").empty().append("你确定完成了发票吗？");
        }else{
            houtai.lishi_invoice.status=0;
            $("#modal_wrap").find(".modal-body").empty().append("你确定改为未完成吗？");
        }
        $("#modal_wrap").show();

    });

    //订单备注按钮
    $("#order_table").on("click", ".mark", function () {
        houtai.linshi_id = $(this).parents("tr").find(".order_number1").text();
        houtai.queren_type = 2;
        if ($(this).parents("td").find(".mark_input").val() === "") {
            houtai.hint("请填写备注！！");
            $(this).parents("td").find(".mark_input").focus();
        } else {
            houtai.remark = $(this).parents("td").find(".mark_input").val();
            $("#modal_wrap").find(".modal-body").empty().append("你确定要为订单<span style='color:#f10;'>" + houtai.linshi_id + "</span>添加一条备注吗？");
            $("#modal_wrap").show();
        }
    });
    // 点击放大图片
    $("#order_table").on("click", ".voucher_img", function () {
        $("#big_img").attr("src","images/loading.gif");
        var img=new Image();
        img.src=$(this).attr("src").replace("_min_","_max_");
        $(img).bind("load",function () {
            $("#big_img").attr("src", img.src);
            var imgH = $(this)[0].naturalHeight;
            var imgW = $(this)[0].naturalWidth;
            var windW = $(window).width();
            var windH = $(window).height();

            if ((((imgW / imgH) > (windW / windH)) && (imgW < windW)) || (((imgW / imgH) <= (windW / windH)) && (imgH < windH))) {
                $("#big_img").css({
                    left: (windW - imgW) / 2,
                    top: (windH - imgH) / 2,
                    width: imgW,
                    height: imgH
                });
            } else if (((imgW / imgH) > (windW / windH)) && (imgW >= windW)) {
                $("#big_img").css({
                    width: windW,
                    height: windW * (imgH / imgW),
                    top: (windH - (windW * (imgH / imgW))) / 2,
                    left: 0
                });
            } else if (((imgW / imgH) <= (windW / windH)) && (imgH >= windH)) {
                $("#big_img").css({
                    width: windH * (imgW / imgH),
                    height: windH,
                    left: (windW - (windH * (imgW / imgH))) / 2,
                    top: 0
                });
            }
        });

        $("#big_img_wrap").fadeIn();


    });
    //图片加载完成时事件
    $("#big_img").bind("load", function () {
        var imgH = $(this)[0].naturalHeight;
        var imgW = $(this)[0].naturalWidth;
        var windW = $(window).width();
        var windH = $(window).height();

        if ((((imgW / imgH) > (windW / windH)) && (imgW < windW)) || (((imgW / imgH) <= (windW / windH)) && (imgH < windH))) {
            $("#big_img").css({
                left: (windW - imgW) / 2,
                top: (windH - imgH) / 2,
                width: imgW,
                height: imgH
            });
        } else if (((imgW / imgH) > (windW / windH)) && (imgW >= windW)) {
            $("#big_img").css({
                width: windW,
                height: windW * (imgH / imgW),
                top: (windH - (windW * (imgH / imgW))) / 2,
                left: 0
            });
        } else if (((imgW / imgH) <= (windW / windH)) && (imgH >= windH)) {
            $("#big_img").css({
                width: windH * (imgW / imgH),
                height: windH,
                left: (windW - (windH * (imgW / imgH))) / 2,
                top: 0
            });
        }
    });
    // 点击大图消失
    $("#big_img_wrap").bind("click", function () {
        $(this).fadeOut();
    });
    //订单修改操作
    $("#order_table").on("change", ".select_state", function () {
        houtai.linshi_id = $(this).parents("tr").find(".order_number>b").text();
        if($(this).val()==="1000"){
            houtai.queren_type = 4;
            $("#modal_wrap").find(".modal-body").empty().append("你确定要把订单改为<span style='color:#f10;'>" + $(this).find("option:selected").text() + "</span>吗？");
            $("#modal_wrap").show();
        }else{
            houtai.queren_type = 3;
            houtai.order_status = $(this).val();
            if ($(this).find("option:selected").text() === $(this).parents("tr").find(".order_state").text()) {
                houtai.hint("此订单的状态已经是<span style='color:#2aabd2;'>" + $(this).find('option:selected').text() + "</span>");
                $(this).val("");
            } else {
                $("#modal_wrap").find(".modal-body").empty().append("你确定要把订单改为<span style='color:#f10;'>" + $(this).find("option:selected").text() + "</span>吗？");
                $("#modal_wrap").show();
            }
        }


    });
    // 查看全部修改历史
    $("#order_table").on("click", ".outbound_all", function () {
        houtai.linshi_id = $(this).parents("tr").find(".order_number1").text();
        if (houtai.click_yes()) {
            houtai.order_history_ajax();
        }
    });
    //订单历史弹框关闭
    $("#close_table_history").bind("click", function () {
        $("#repertory_table_history").fadeOut(200);
    });
    // 打印
    $("#order_table").on("click", ".Excel", function () {
        if (houtai.click_yes()) {
            houtai.print_ajax($(this).parents("tr").find(".order_number>b").text());
        }
    });

    //订单修改确认操作
    $("#confirm").bind("click", function () {
        if (houtai.click_yes()) {
            if(houtai.queren_type===2){
                houtai.add_remark_ajax();
            }else if(houtai.queren_type===3){
                houtai.set_order_ajax();
            }else if(houtai.queren_type===4){
                houtai.tuikuan_ajax();
            }else if(houtai.queren_type===5){
               houtai.set_invoice_ajax();
            }

        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);
        $(".select_state").val("");

    });
    // 按时间搜索
    $("#search_time").bind("click", function () {
        if ($("#input_time").val() === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.linshi_search.time_start = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.linshi_search.time_end = new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.linshi_search.id = "";
                houtai.linshi_search.pay_way = null;
                houtai.linshi_search.is_refund = null;
                houtai.order_list_ajax();
            }
        }

    });
    //搜索订单按钮
    $("#search_all").bind("click", function () {
        if ($("#search_input").val() === "") {
            $("#search_input").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                houtai.linshi_search.payment_state = "";
                houtai.linshi_search.time_end = "";
                houtai.linshi_search.time_start = "";
                houtai.linshi_search.id = $.trim($("#search_input").val());
                houtai.linshi_search.pey_way = null;
                houtai.linshi_search.is_refund = null;
                houtai.order_list_ajax();
            }
        }

    });
    // 回车事件
    $("#form_search").submit(function () {
        if ($("#search_input").val() === "") {
            $("#search_input").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                houtai.linshi_search.payment_state = "";
                houtai.linshi_search.time_end = "";
                houtai.linshi_search.time_start = "";
                houtai.linshi_search.id = $.trim($("#search_input").val());
                houtai.linshi_search.pey_way = null;
                houtai.linshi_search.is_refund = null;
                houtai.order_list_ajax();
            }
        }
        return false;
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});

